package io.github.talelin.latticy.controller.v1.ag;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.ag.monitoringdevices.CreateOrUpdateMonitoringDevicesDTO;
import io.github.talelin.latticy.model.ag.MonitoringDevicesDO;
import io.github.talelin.latticy.model.ag.MonitoringDevicesDetailDO;
import io.github.talelin.latticy.service.ag.MonitoringDevicesService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;
import java.util.List;

@RestController
@Validated
@RequestMapping("/v1/ag/monitoringdevices")
public class MonitoringDevicesController {
    @Autowired
    private MonitoringDevicesService mdService;
//    @Autowired
//    private MonitoringDevicesMapper mdMapper;
//
    @GetMapping("/{id}")
    public MonitoringDevicesDetailDO getMD(@PathVariable(value = "id")@Positive(message = "id必须为正整数")int id){
        MonitoringDevicesDetailDO mdevice = mdService.getDetailById(id);
        if (mdevice==null){
            throw new NotFoundException("未找到监控设备",10023);
        }
        return mdevice;
    }

    @GetMapping("/list")
    public List<MonitoringDevicesDO> getMDList(){
        List<MonitoringDevicesDO> MonitoringDevices=mdService.getMonitoringDevices();
        if (MonitoringDevices==null){
            throw new NotFoundException("未找到监控设备");
        }
        return MonitoringDevices;
    }
    @GetMapping("/page")
    public PageResponseVO<MonitoringDevicesDetailDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{count}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page}") Integer page
    ) {
        io.github.talelin.latticy.common.mybatis.Page<MonitoringDevicesDetailDO> pager = new Page<>(page, count);
        IPage<MonitoringDevicesDetailDO> paging =mdService.getMonitoringDeveicesByPage(pager);
        return PageUtil.build(paging);
    }
    @GetMapping("/search")
    public PageResponseVO<MonitoringDevicesDetailDO> search(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{count}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page}") Integer page,
            @RequestParam(name = "keyword") String keyword
    ) {
        Page<MonitoringDevicesDetailDO> pager = new Page<>(page, count);
        IPage<MonitoringDevicesDetailDO> paging = mdService.searchRoomByKeyword(pager, keyword);
        return PageUtil.build(paging);
    }

    @PostMapping("/create")
    public CreatedVO createMD(@RequestBody @Validated CreateOrUpdateMonitoringDevicesDTO validator){
        mdService.createMonitoringDevices(validator);
        return new CreatedVO("创建监控设备成功");
    }
    @PutMapping("/{id}")
    public UpdatedVO updatedMD(@PathVariable("id") @Positive(message = "{id.positive}") Integer id,
                               @RequestBody @Validated CreateOrUpdateMonitoringDevicesDTO validator){
        MonitoringDevicesDO monitoringDevices = mdService.getById(id);
        if (monitoringDevices == null){
            throw new NotFoundException("MonitoringDevices not found",10022);
        }
        mdService.updateMonitoringDevieces(monitoringDevices,validator);
        return new UpdatedVO("更新监控设备成功");
    }

    @DeleteMapping("/{id}")
    @PermissionMeta(value = "删除监控设备", module = "监控设备", mount = true)
    @GroupRequired
    public DeletedVO deletedMD(@PathVariable("id") @Positive(message = "{id}") int id){
        MonitoringDevicesDO md = mdService.getById(id);
        if (md==null){
            throw new NotFoundException("未找到监控设备",10022);
        }
        mdService.deleteMonitoringDevices(md.getId());
        return new DeletedVO(16);
    }


}
